/*
 * @Author: kanglang
 * @Date: 2021-02-23 11:22:57
 * @LastEditors: zeki mini
 * @LastEditTime: 2021-06-10 14:27:19
 * @Description: 企业认证反馈页面
 */

import React from 'react';
import { View, StyleSheet, Image, Text, SafeAreaView } from 'react-native';
import { Button, msg, AsyncStorage, cache } from '@/common';
import { px2dp, mainBgColorWhite, fontColorBlack } from '@/styles';
import { iconRightSmall, iconError } from '@/images';
import businessApi from '@/api/business';

const { Submit } = Button;

export default class BusinessCertiFeedback extends React.Component {
  constructor(props) {
    super(props);
    const { navigation } = props;
    const state = navigation.state || {};
    const params = state.params || {};
    const { status, reason = '', routerFrom } = params;
    this.state = {
      status,
      reason,
      legalInfo: {},
      idCard: {},
      bankcard: {},
      admin: {},
      routerFrom
    };
  }

  componentDidMount() {
    AsyncStorage.getItem(cache.BUSINESS_INFO_CERTI).then((data) => {
      const dataCache = JSON.parse(data) || {};
      const { businessInfoCerti } = dataCache;
      this.setState({ legalInfo: businessInfoCerti });
    }).catch(err => {
      console.log('企业信息反写异常：', err);
    });

    AsyncStorage.getItem(cache.LEGAL_INFO_CERTI).then((data) => {
      const dataCache = JSON.parse(data) || {};
      const { legalInfoCerti } = dataCache;
      this.setState({ idCard: legalInfoCerti });
    }).catch(err => {
      console.log('法人信息反写异常：', err);
    });

    AsyncStorage.getItem(cache.CORPORATE_ACCOUNT_INFO_CERTI).then((data) => {
      const dataCache = JSON.parse(data) || {};
      const { CorporateAccountInfoCerti } = dataCache;
      this.setState({ bankcard: CorporateAccountInfoCerti });
    }).catch(err => {
      console.log('对公账户信息反写异常：', err);
    });

    AsyncStorage.getItem(cache.BUSINESS_MANAGE_SET_CERTI).then((data) => {
      const dataCache = JSON.parse(data) || {};
      const { BusinessManageSetCerti } = dataCache;
      this.setState({ admin: BusinessManageSetCerti });
    }).catch(err => {
      console.log('企业管理员信息反写异常：', err);
    });
  }

  render() {
    const { status, reason } = this.state;
    return (
      <SafeAreaView style={styles.container}>
        <View style={styles.inner}>
          <View style={styles.upBox}>
            <Image
              style={styles.finishStatusIcon}
              source={status ? iconRightSmall : iconError}
            />
            <Text style={styles.title}>{status ? '您的企业认证资料已提交' : '您的企业认证资料提交失败'}</Text>
            <View style={styles.secondTitleBox}>
              {
                status ?
                  <Text style={styles.secondTitle}>我们将在1-3个工作日内审核您上传的文件，审核结果将通过短信或电话的途径通知您，您也可以通过登录APP查看审核结果。</Text> :
                  <Text style={styles.secondTitle}>{`失败原因：${reason}`}</Text>
              }
            </View>
          </View>
          <View style={styles.btnBox}>
            <Submit
              text={status ? '返回企业中心' : '重新提交'}
              btnTextStyle={styles.nextBoxStyle}
              boxStyle={styles.nextBoxStyle}
              onClick={this.handleMethor}
            />
            {
              !status &&
              <Submit
                text='返回企业中心'
                btnTextStyle={styles.preStepText}
                boxStyle={styles.preBoxStyle}
                onClick={this.goHome}
              />
            }
          </View>
        </View>
      </SafeAreaView>
    );
  }

  // 成功 返回企业中心  失败 重新提交
  handleMethor = () => {
    const { status } = this.state;
    if (status) {
      this.goHome();
    } else {
      const { admin, legalInfo, idCard, bankcard } = this.state;
      // 重新提交 刷新当前页面
      const certiParams = {
        admin,
        legalInfo,
        idCard,
        bankcard
      };
      businessApi.addLegal(certiParams).then(res => {
        if (res.success) {
          this.setState({
            status: true
          });
        } else {
          this.setState({
            status: false,
            reason: res.msg
          });
        }
      });
    }
  }

  goHome = () => {
    const { routerFrom } = this.state;
    const cacheList = [
      cache.BUSINESS_INFO_CERTI,
      cache.LEGAL_INFO_CERTI,
      cache.CORPORATE_ACCOUNT_INFO_CERTI,
      cache.BUSINESS_MANAGE_SET_CERTI
    ];
    cacheList.forEach(key => {
      AsyncStorage.setItem(key, '');
    });
    if (routerFrom === 'B2B') { // 从B2B来
      msg.emit('router: refreshRoute', {
        routeName: 'Tab',
      });
      msg.emit('router: goToNext', {
        routeName: 'Tab'
      });
    } else { // 从保险来
      msg.emit('router: refreshRoute', {
        routeName: 'InsuranceTab',
      });
      msg.emit('router: goToNext', {
        routeName: 'InsuranceTab'
      });
    }
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: mainBgColorWhite,
  },
  inner: {
    flex: 1,
  },
  upBox: {
    alignItems: 'center'
  },
  finishStatusIcon: {
    width: px2dp(294),
    height: px2dp(294),
    marginTop: px2dp(64)
  },
  title: {
    fontSize: px2dp(40),
    color: fontColorBlack
  },
  secondTitleBox: {
    paddingHorizontal: px2dp(105),
    marginTop: px2dp(24),
    color: '#9D9A95'
  },
  secondTitle: {
    color: '#9D9A95',
    fontSize: px2dp(28)
  },
  btnBox: {
    width: '100%',
    alignItems: 'center',
    position: 'absolute',
    bottom: px2dp(200)
  },
  nextBoxStyle: {
    width: px2dp(520),
    borderRadius: px2dp(12),
  },
  nextStepText: {
    fontSize: px2dp(32),
  },
  preBoxStyle: {
    width: px2dp(520),
    borderRadius: px2dp(12),
    backgroundColor: '#E6E6E5',
    marginTop: px2dp(40)
  },
  preStepText: {
    fontSize: px2dp(32),
    color: '#686764',
  },

});
